// pagesProperty/pages/asset/borrow/index.js
var app = getApp(), fetchApi = require("../../../../../utils/httpPost.js"),
formatTime = require("../../../../../component/common.js");
import Toast from '../../../../../component/vant/toast/toast';
import Dialog from '../../../../../component/vant/dialog/dialog';

var common_Js = require("../../../../../component/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetId:0,
    isLoading: true,
    is_estate_user:0,
    assetInfo:{},
    borrowInfo:{},
    submitState: false,
    borrowRecordId:0,    
    fileList1:[],
    jd:0,
    wd:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('borrow', options);
    if(options.id){
      this.setData({
        borrowRecordId: options.id,
      })
    }
    this.modalcnt();
  },  
  modalcnt:function  ()  {
    var  that  =  this;
    //获取经纬度  
    wx.getLocation({
      type:  'gcj02',
      success: function  (res)  {
        console.log('modalcnt', res)
        var latitude = res.latitude
        var longitude = res.longitude
        that.data.wd = latitude//纬度
        that.data.jd = longitude//经度
        that.setData({
          wd : latitude,
          jd : longitude
        })
        that.onHttpInit();
      },fail:function(res){
        that.data.wd = 0;
        that.data.jd = 0;
        that.onHttpInit();
      }
    })
  },
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/borrow/record/detail', {
      borrow_record_id: that.data.borrowRecordId,
    }, function (res) {
      console.log("获取资产借用记录信息", res)
      if (res.data.code == 0) {
        that.setData({
          hasData: true,
          assetInfo: res.data.data.asset,
          borrowInfo: res.data.data.borrowInfo,
        })
        that.data.assetId = res.data.data.asset.id;
      }else{
        Toast.fail(res.data.msg)
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
  deleteImg:function(res){
    console.log('删除图片', res)
    var index = res.currentTarget.dataset.index;
    this.data.fileList1 = [];
    this.setData({
      fileList1: this.data.fileList1
    });
  },
  afterRead(event) {
    console.log('afterRead',event)
    const { file, name } = event.detail;
    console.log('afterRead name',name)
    console.log('afterRead file',file)
    const fileList = this.data[`fileList${name}`];    
    this.setData({ [`fileList${name}`]: fileList.concat(file) });    
    console.log('afterRead fileList',fileList)
    this.data.fileList1.concat(file)
    console.log('fileList',this.data.fileList1)
  },
  oversize() {
    wx.showToast({ title: '文件超出大小限制', icon: 'none' });
  },
  goReback(){
    this.onHttpSubmit();
  },
  onHttpSubmit: function () {
    var that = this;
    fetchApi.httpPost('/estate/asset/reback', {
      asset_id: that.data.assetId,
      borrow_record_id: that.data.borrowRecordId,
      lng: that.data.jd,
      lat: that.data.wd,
    }, function (res) {
      console.log("获取资产信息", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        if (that.data.borrowRecordId > 0 && that.data.fileList1.length > 0) {
          for (var index in that.data.fileList1) {
            wx.uploadFile({
            url: fetchApi.getPlatformUrl() + '/estate/asset/reback/uploadImg',
            filePath: that.data.fileList1[index].url,
            name: 'file',
            formData: {
              record_id: that.data.borrowRecordId,
            },
            success: function (res) {
              console.log('uploadFile success res', res)
            }, fail: function (res) {
              console.log('uploadFile fail res', res)
            }
            })
          }
        }
        setTimeout(function(){
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
          prevPage.setData({
            currentTab: 1,
          })
          wx.navigateBack({ delta: 1 });
        }, 300);
      }else{
        Toast.fail(res.data.msg)
      }      
      that.data.submitState = false;
      that.setData({submitState : false })
      that.setData({ netWork: true,loading: true,  isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false,loading: true,  isLoading: false, })
    })
  },
})